import Vue from 'vue'
import VueRouter from 'vue-router'
import home from './home'
import detail from './detail'
import order from './order'
import store from '../store'
Vue.use(VueRouter)

import cart from './cart'

import produc from './produc'

Vue.use(VueRouter)

const routes = [
    ...cart,
    ...home,
    ...produc,
    ...detail,
    ...order
]

const router = new VueRouter({
    routes
})

// let isLogin = false //模拟登录状态
router.beforeEach((to, from, next) => {
    // to  用户期望访问的目标路由信息
    // from  用户来源的目标路由信息
    // next  放行用户进入下一级路由
    // console.log(to,from);
    let { userInfo } = store.state.user //还是解构
    let path = to.path
    if (path.indexOf('/cart') != -1 || path.indexOf('/personal') != -1) { //判断用户访问的是否为个人中心路由
        //判断用户是否登录
        if (!userInfo) {
            next({ //设置路由参数
                path: '/login',
                query: {
                    target: to.path
                }
            })
        } else {
            next()
        }
    } else {
        next()
    }
})
export default router